package org.springblade.modules.platform.controller;


import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;

import org.springblade.modules.platform.dto.*;
import org.springblade.modules.platform.entity.SysJob;
import org.springblade.modules.platform.job.TaskException;
import org.springblade.modules.platform.service.ISysJobService;
import org.springblade.modules.platform.utils.CronUtils;
import org.springblade.modules.platform.vo.SysJobListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.quartz.SchedulerException;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@AllArgsConstructor
@RequestMapping("/service-huateng-sxdb/job")
@Api(value = "", tags = "定時器")
@Slf4j
public class SysJobController {
    private final ISysJobService jobService;


    @PostMapping(value = "/install")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "新增", notes = "传入jobId")
    public R add(@RequestBody SysJobDTO sysJobDTO) throws Exception{
        SysJob job = new SysJob();
        job.setJobGroup("DEFAULT");
        job.setCronExpression(sysJobDTO.getCronExpression());
        job.setMisfirePolicy("3");
        job.setConcurrent("1");
        job.setInvokeTarget(sysJobDTO.getInvokeTarget());
        job.setJobName(sysJobDTO.getJobName());
        job.setCreateBy(AuthUtil.getNickName());
        job.setCreateTime(new Date());
		jobService.insertJob(job);
        return R.success("成功");
    }
    @GetMapping("/list")
    public R list(SysJobDTO sysJobDTO,Query query) {
        SysJob sysJob = new SysJob();
        sysJob.setCronExpression(sysJobDTO.getCronExpression());
        sysJob.setJobName(sysJobDTO.getJobName());
        SysJobListVO sysJobListVO = jobService.selectJobList(sysJob, query);
        return R.data(sysJobListVO);
    }
    @PutMapping("/updateState")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "启用/禁用", notes = "传入ruleLinkageUpdateDTO")
    public R updateState(@RequestBody SysJobUpdataStateDTO sysJobUpdataStateDTO)throws SchedulerException{
        SysJob sysJob = jobService.selectJobById(sysJobUpdataStateDTO.getJobId());
        sysJob.setStatus(sysJobUpdataStateDTO.getStatus());
        jobService.changeStatus(sysJob);
        return R.success("成功");
    }




    @PutMapping("/update")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "修改", notes = "传入SysJobDTO")
    public R edit(@RequestBody SysJobDTO sysJobDTO) throws SchedulerException, TaskException {
        if (!CronUtils.isValid(sysJobDTO.getCronExpression())) {
            return R.fail("cron表达式不正确");
        }
        SysJob sysJob = jobService.selectJobById(sysJobDTO.getJobId());
        sysJob.setCronExpression(sysJobDTO.getCronExpression());
        sysJob.setJobName(sysJobDTO.getJobName());
        jobService.updateJob(sysJob);
        return R.success("成功");
    }

    @DeleteMapping("/remove/{id}")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "删除", notes = "传入id")
    public R remove(@PathVariable Long id) throws Exception{
        jobService.deleteJobById(id);
        return R.success("成功");
    }
}
